package com.showdor.springboot;

import com.alibaba.fastjson2.JSONObject;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import javax.annotation.Resource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.*;

/**
 * @author showdor
 * @email chuanqi@outlook.com
 * @date 2023/10/20
 */
@Configuration    // 1. 代表当前类是一个配置类
@EnableScheduling // 2.开启定时任务
public class ScheduleTask {
    @Resource
    private KafkaTemplate<String, Object> kafkaTemplate;

    @Scheduled(cron = "10 * * * * ?")
    public void testTopicProducer() {
        List<JSONObject> list = new ArrayList<>();
        String topic = "testTopic";
        long ts = System.currentTimeMillis() - 1_723_000_000_000L;
        int num = new Random().nextInt(1000);
        for (int i = 0; i <= num; i++) {
            // String str="{\"data\":[{\"MotorVehicleID\":\""+i+"\",\"InfoKind\":\"1\",\"SourceID\":\"1\",\"TollgateID\":\"1\",\"DeviceID\":\"1\",\"StorageUrl1\":\"浙A123457_01.jpg\",\"StorageUrl2\":\"浙A123457_01.jpg\",\"StorageUrl3\":\"1\",\"StorageUrl4\":\"1\",\"StorageUrl5\":\"1\",\"MarkTime\":\"\",\"LaneNo\":\"1\",\"PlateClass\":\"1\",\"PlateColor\":\"1\",\"PlateNo\":\"浙A"+i+"\",\"PlateNoAttach\":\"1\",\"PlateDescribe\":\"1\",\"Speed\":\"1.0\",\"Direction\":\"1\",\"UsingPropertiesCode\":\"1\",\"VehicleClass\":\"1\",\"VehicleBrand\":\"1\",\"VehicleModel\":\"1\",\"VehicleColor\":\"1\",\"create_time\":\"2023-07-04 10:09:55\",\"PassTime\":\"20230713190307\",\"NameOfPassedRoad\":\"1\",\"ThirdId\":\"1\"}]}";
            JSONObject jo = new JSONObject();
            JSONObject jo2 = new JSONObject();
            jo2.put("id", ts + i);
            jo.put("order", jo2);
            jo.put("order_id", i + new Random().nextInt(100));
            jo.put("order_date", new Date());
            jo.put("customer_name", "test " + i);
            jo.put("price", new Random().nextDouble() * 100);
            jo.put("product_id", i);
            jo.put("order_status", 1);
            list.add(jo);
            //向kafka的big_data_topic主题推送数据
            kafkaTemplate.send(topic, jo.toJSONString());
           // kafkaTemplate.send(topic, JSONObject.toJSONString(list));
        }
       // System.out.println("发送消息到kafka");
        //System.out.println(JSONObject.toJSONString(list));
        //kafkaTemplate.send(topic, JSONObject.toJSONString(list));
    }


}
